#include "snowboy/snowboy-detect-c-wrapper.h"
#include <stdlib.h>
#include <stdio.h>
#include "record.h"
#include "stt.h"
#include "chat.h"
#include "tts.h"

int main()
{

    char* text;
    //创建snowboy检测器
    SnowboyDetect* detector = SnowboyDetectConstructor("common.res", "computer.umdl");
    if (!detector)
    {
        return EXIT_FAILURE;
    }

    //获取检测器支持的音频数据参数
    int bits = SnowboyDetectBitsPerSample(detector);
    int channels = SnowboyDetectNumChannels(detector);
    int rate = SnowboyDetectSampleRate(detector);

    printf("采样深度: %d\n", bits);
    printf("声道数量: %d\n", channels);
    printf("采样频率: %d\n", rate);

    //打开音频采集设备
    snd_pcm_uframes_t period = 999;
    snd_pcm_t* capture = record_open("hw:0,1", SND_PCM_FORMAT_S16_LE, channels, rate, &period);
    if (!capture)
    {
        return EXIT_FAILURE;
    }   

    char* buffer = malloc(snd_pcm_frames_to_bytes(capture, period)); // 分配缓冲区
    if (!buffer) {
        perror("malloc");
        record_close(capture);
        SnowboyDetectDestructor(detector);
        return EXIT_FAILURE;
    }

    int recording = 0;
    int silence = 0;
    int silence1 = 0;
    FILE* memstream = NULL;
    char* audio = NULL;
    size_t audio_size = 0;
    int wakeword_detected = 0; // 标志变量，指示是否已经检测到唤醒词
    int flag = 0;
    int flag1 = 0;
    int flag2 = 0 ;
    

    while (1)
    {   flag1=0;
        snd_pcm_sframes_t frames = snd_pcm_readi(capture, buffer, period); // 从PCM设备读取数据
        if (frames < 0)
        {
            fprintf(stderr, "Error from read: %s\n", snd_strerror(frames));
            snd_pcm_recover(capture, frames, 0);
            continue;
        }

        int status = SnowboyDetectRunDetection(detector, (int16_t*)buffer, snd_pcm_frames_to_bytes(capture, frames) / sizeof(int16_t), 0);
        
        if (!wakeword_detected) // 如果还没有检测到唤醒词
        {
            
            if (status > 0)
            {
                printf("检测到唤醒词，开始录音\n");
                recording = 1;
                memstream = open_memstream(&audio, &audio_size);
                if (!memstream)
                {
                perror("open_memstream");
                continue;
                }
            }
        }
        else // 如果已经检测到唤醒词，则直接录音
        {
            if ( !recording ) // 开始新的录音
            {
                
                if(!flag1 )
                {
                    if (status == -2)
                    {
                        silence1++;
           
                    }
                    else
                    {
                        silence1 = 0;
                        flag = 1;
                        flag1 = 1;
                    }

                    if (silence1 > 96)//放弃录音等待唤醒词
                    {
                        printf("放弃录音等待唤醒词\n");
                        silence1 = 0;
                        flag2 = 1;
                        wakeword_detected = 0;
                        flag1 = 1;
                        if (memstream)
                        {
                            fclose(memstream);
                            memstream = NULL;
                        }
                    }
                }
                if (flag2 )//进行再次录音
                {
                    recording = 0;
                    flag2=0;
                }
                if (flag)
                {
                    recording = 1;
                    flag=0;//更新
                }

                memstream = open_memstream(&audio, &audio_size);
                if (!memstream)
                {
                    perror("open_memstream");
                    continue;
                }
            }
        
    }

    if (recording)
    {
        if (status == -2)
        {
            silence++;
           
        }
        else
        {
            silence = 0;
            
        }

        if (silence > 32)
        {
            printf("停止录音\n");
            recording = 0;
            silence = 0;
            if (memstream)
            {
                fclose(memstream);
                memstream = NULL;
            }

            if (audio_size)
            {
                snd_pcm_drop(capture);
               
                // 识别语音
                char* text = speech_to_text(audio, audio_size);
                if (text)
                {
                    printf("识别结果: %s\n", text);
                    char* answer = print_text(text);
                    text_to_speech(answer);
                    wakeword_detected = 1;
                }
                snd_pcm_prepare(capture);
            }
        }

        if (memstream)
        {
            fwrite(buffer, 1, snd_pcm_frames_to_bytes(capture, frames), memstream);
        }
    }
}

free(buffer);
record_close(capture);
SnowboyDetectDestructor(detector);

return EXIT_SUCCESS;
}
